package me.yobol.greentable.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.yobol.greentable.bean.DishesBean;
import me.yobol.greentable.service.DishesService;

/**
 * @author Yobol
 * 
 */
public class GetDishesListServlet extends HttpServlet {

	private static final long serialVersionUID = -467058083955114141L;

	public GetDishesListServlet() {
		super();
	}

	public void init() throws ServletException {
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int flag = (request.getParameter("flag") == null) ? -1 : Integer
				.parseInt(request.getParameter("flag"));
		int dishesOwner = (request.getSession().getAttribute("uId") == null) ? -1
				: (Integer) request.getSession().getAttribute("uId");

		String forwardUrl = null;
		if (flag == 0) {
			List<DishesBean> allDishes = DishesService.getAllDishes();
			request.getSession().setAttribute("dishes_list", allDishes);
			forwardUrl = "/pages/dishes_list.jsp";
		} else {
			List<DishesBean> dsByOwner = DishesService
					.getDishesByOwnerId(dishesOwner);
			request.getSession().setAttribute("dishes_list", dsByOwner);
			forwardUrl = "/pages/dishes_list.jsp";
			
		}
		request.setAttribute("flag", flag);
		RequestDispatcher rd = request.getRequestDispatcher(forwardUrl);
		rd.forward(request, response);
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

}
